class SGW
  #limits for SGW: max IP bearers (max users), IP packets forwarding rate, eNodeB to SGW ratio
  #each SGW can forward up to 100 mbit/s and support up to 100 users
  SGW_user_capacity = 100
  SGW_traffic_capacity = 100.0
  
  #pool number, SGW ID, load
  attr_accessor :pools, :id, :traffic, :users

  def initialize(pool,id)
    @pools = Array.new()
    @id = id
    @traffic = 0.0
    @users = 0.0
  end

  def overload_by_users
    @users - SGW_user_capacity
  end

  def overload_by_traffic
    @traffic - SGW_traffic_capacity
  end

  def handover(source_pool_index,target_pool_index,target_sgw_index)

    if self.users>SGW_user_capacity
      delta=[self.users-SGW_user_capacity,SGW_user_capacity-$pools[target_pool_index].sgws[target_sgw_index].users].min
    else
      delta=[(self.users/self.pools.size.to_f).floor,[SGW_user_capacity-$pools[target_pool_index].sgws[target_sgw_index].users,0].max].min
    end

    $pools[target_pool_index].sgws[target_sgw_index].users+=delta
    $pools[target_pool_index].sgws[target_sgw_index].traffic+=$cbr*delta
    $pools[source_pool_index].sgw_users_overload-=delta
    $pools[source_pool_index].sgw_traffic_overload-=$cbr*delta
    self.users-=delta
    self.traffic-=$cbr*delta

    p "Handed over #{$cbr*delta} megabits from pool #{$pools[source_pool_index].id} SGW #{id} to pool #{$pools[target_pool_index].id} SGW #{$pools[target_pool_index].sgws[target_sgw_index].id}"
    unless $pools[target_pool_index].sgws[target_sgw_index].pools.include?($pools[source_pool_index].id)
        #add the target SGW to source pool
        $pools[source_pool_index].add_sgw($pools[target_pool_index].sgws[target_sgw_index])
        p "SGW #{$pools[target_pool_index].sgws[target_sgw_index].id} is now included in pool #{$pools[source_pool_index].id}"
    end
  end
  
end